package com.tao.lab;

/**
 * @description: brance-download
 * @author: wangxf
 * @date: 2023/7/11 18:31
 */

import org.junit.Test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MovieCrawler {
    @Test
    public static void main(String[] args) {
        String url = "https://www.hao6v.tv/";
        try {
            String htmlContent = downloadHtml(url);
            System.out.println(htmlContent);
            if (htmlContent != null) {
                extractMovieLinks(htmlContent);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static String downloadHtml(String url) throws IOException {
        StringBuilder content = new StringBuilder();
        HttpURLConnection connection = null;
        BufferedReader reader = null;
        try {
            URL requestUrl = new URL(url);
            connection = (HttpURLConnection) requestUrl.openConnection();
            connection.setRequestMethod("GET");

            reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                content.append(line);
            }
        } finally {
            if (reader != null) {
                reader.close();
            }
            if (connection != null) {
                connection.disconnect();
            }
        }
        return content.toString();
    }

    private static void extractMovieLinks(String htmlContent) {
        Pattern pattern = Pattern.compile("<a href=\"(.*?)\">");
        Matcher matcher = pattern.matcher(htmlContent);
        while (matcher.find()) {
            String link = matcher.group(1);
            if (link.contains(".jpg")) {
                System.out.println(link);
            }
        }
    }
}
